﻿using PFMCandidate;
using PFMConfigurations;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PFMFinders
{
    public class PFMFileFinder:PFMFinder
    {
        List<String> sFiles = null;
        public int iIndex = 0;
        public PFMFileFinder()
        {
        }
        public PFMFileFinder(Target aTarget)
            : base(aTarget)
        {
            sFiles = new List<string>();
            if (aTarget.Subdirectory)
                GetAllDirectoryFiles(aTarget);
            else
                sFiles.AddRange(Directory.GetFiles(aTarget.Location, aTarget.Filter));
        }
        /// <summary>
        /// 获得下一个 Candidate 对象
        /// </summary>
        /// <returns></returns>
        public override Candidate FindNext()
        {
            Candidate Result = null;
            if (iIndex < sFiles.Count)
            {
                Result = CreateFileCandidate(sFiles[iIndex]);
                iIndex++;
            }
            return Result;

        }
        /// <summary>
        /// 内部索引归零 
        /// </summary>
        public void Reset()
        {
            this.iIndex = 0;
        }
        /// <summary>
        /// 依据文件名返回创建好的Candidate 对象 
        /// </summary>
        /// <param name="sFileName">文件的完整路径</param>
        /// <returns></returns>
        protected Candidate CreateFileCandidate(string sFileName)
        {
            FileInfo aFi;
            Candidate Result = null;
            if (File.Exists(sFileName))
            {
                aFi = new FileInfo(sFileName);
                //Result = new Candidate(o_Target, sFileName, aFi.CreationTime, aFi.Length);
                Result = CandidateFactory.CreateCandidate(o_Target, sFileName, aFi.CreationTime, aFi.Length);
               //Result = new Candidate() { Name =aFi.DirectoryName+@"\"+ aFi.Name, Size = aFi.Length, FileDateTime = aFi.CreationTime, ProcessedName = aFi.DirectoryName };
            }
            return Result;
        }
        /// <summary>
        /// 获得目录下符合条件的文件（包含子目录），并放置在 sFiles 里面 
        /// </summary>
        protected void GetAllDirectoryFiles(Target aTarget)
        {
            sFiles = new List<string>();
            sFiles.AddRange(Directory.GetFiles(aTarget.Location, aTarget.Filter, SearchOption.AllDirectories).Where(t => t.ToLower().EndsWith(".zip")));

        }
        /// <summary>
        /// 获取数组大小 
        /// </summary>
        /// <returns></returns>
        public override int GetCount()
        {
            return sFiles.Count();
            //throw new NotImplementedException();
        }
        /// <summary>
        /// 获取指定索引处的文件对象，并封装成 Candidate 对象返回 
        /// </summary>
        /// <param name="iIndex">指定索引处</param>
        /// <returns></returns>
        public override Candidate GetItem(int iIndex)
        {
            Candidate Result = null;
            if (iIndex < sFiles.Count)
                Result = CreateFileCandidate(sFiles[iIndex]);
            return Result;
        }
    }
}
